package cn.hsa.xxx.approve.service.impl;

import cn.hsa.hsaf.core.mq.MQMessage;
import cn.hsa.hsaf.core.mq.MQProducer;
import cn.hsa.xxx.api.dto.ShelvesApprovalProcessDTO;
import cn.hsa.xxx.approve.service.ProducerService;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.beans.factory.annotation.Value;
import org.springframework.stereotype.Service;

@Service
@Slf4j
public class ProducerServiceImpl implements ProducerService {

    @Value("${mq.topic.dto}")
    private String dtoTopic;

    @Autowired
    private MQProducer mqProducer;


    @Override
    public boolean sendMsgDTO(ShelvesApprovalProcessDTO shelvesApprovalProcessDTO) {

        MQMessage<ShelvesApprovalProcessDTO> mqMessage = new MQMessage<>();
        mqMessage.setTopic(dtoTopic);
        mqMessage.setTag("");
        mqMessage.setContent(shelvesApprovalProcessDTO);
        return execute(mqMessage);
    }



    private boolean execute(MQMessage<?> mqMessage) {
        if(mqProducer.send(mqMessage)) {
            log.info("员工发起上架请求，发送成功，消息主题：{}，消息内容：{}。", mqMessage.getTopic(), mqMessage);
            return true;
        }
        log.error("员工发起上架请求，发送失败，消息主题：{}，消息内容：{}。", mqMessage.getTopic(), mqMessage);
        return false;
    }
}
